package com.cgs.query.domain

import com.cgs.query.util.Utils
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import java.util.*
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
import kotlin.collections.ArrayList

@Entity
@Table(name = "query_project")
@ApiModel("项目(分类)")
class QueryProject() : BaseDomain() {

    @Id
    @ApiModelProperty("名称")
    var name: String = ""
    @Column
    @ApiModelProperty("数据源ID")
    var sourceId: String = ""
    @Column
    @ApiModelProperty("锁定方式")
    var lockType: String = LockType.OPEN.name

    constructor(name: String, lockType: String) : this() {
        this.name = name
        this.lockType = lockType
    }

    fun copy(): QueryProject {
        return QueryProject().let {
            it.name = this.name
            it.sourceId = this.sourceId
            it.lockType = this.lockType
            it.userId = this.userId
            it.creationTime = this.creationTime
            it.updateTime = this.updateTime
            it.remark = this.remark
            return@let it
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is QueryProject) return false

        if (name != other.name) return false

        return true
    }

    override fun hashCode(): Int {
        return name.hashCode()
    }

}

@ApiModel("项目中定义包装类")
class ProjectDefineVo(val project2Define: Project2Define,
                      val defineName: String,
                      val defineRemark: String) {
    @ApiModelProperty("调用名称")
    var callName: String = getWholeName()

    private fun getWholeName(): String {
        return if (project2Define.alias.isBlank()) {
            defineName
        } else {
            Utils.packageAliasAndDefineName(project2Define.alias, defineName)
        }
    }
}

/**
 * 项目锁定
 */
enum class LockType {
    /**不锁定*/
    OPEN,
    /**锁定但能执行*/
    LOCK_RUN,
    /**锁定不能执行*/
    LOCK_UN_RUN;
}

/**
 * 一个完整项目
 */
class CompleteProject {
    /**一个数据源*/
    var querySource: QuerySource? = null
    /**一个项目*/
    var queryProject: QueryProject? = null
    /**定义集合*/
    var queryDefines: List<QueryDefine>? = null
    /**执行对象集合*/
    var executionObjs: List<ExecutionObj>? = null
    /**项目定义联系*/
    var project2Defines: List<Project2Define>? = null
}

/**
 * 项目需要被同步的部分
 */
class SyncProject() {
    /**定义集合*/
    var queryDefines: List<QueryDefine>? = null
    /**执行对象集合*/
    var executionObjs: List<ExecutionObj>? = null
    /**项目定义联系*/
    var project2Defines: List<Project2Define>? = null

    constructor(queryDefines: List<QueryDefine>?, executionObjs: List<ExecutionObj>?, project2Defines: List<Project2Define>?) : this() {
        this.queryDefines = queryDefines
        this.executionObjs = executionObjs
        this.project2Defines = project2Defines
    }
}